/**************************************************************************//**
 * @file     sensor_nt99141.c
 * @version  V1.00
 * @brief    NT99141 sensor driver
 *
 * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
 ******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "i2c_gpio.h"

struct NT_RegValue
{
    uint16_t    u16RegAddr;            /* Sensor Register Address */
    uint8_t        u8Value;            /* Sensor Register Data */
};


static struct NT_RegValue hm1055_setting_YUV_720P[] = {
/* HM1055 , I2C Slave address : 0x24
 * PWDN = Low (Preview mode) ; = High(Power down)
 * Reset = High (Preview mode) ; =Low (Reset)
 */

/* 1280 x 720 , YUV mode ,frame rate = 30fps */
{0x0022, 0x00}, {0x0023, 0xCF}, {0x0020, 0x08}, {0x0027, 0x30},
{0x0004, 0x10}, {0x0006, 0x03}, {0x0012, 0x0F},
/* {0x0026, 0x77}, */ /*48Mhz */
{0x0026, 0x37}, /*68Mhz */
{0x002A, 0x44}, {0x002B, 0x01}, {0x002C, 0x00}, /* {0x0025, 0x00}, */
{0x004A, 0x0A}, {0x004B, 0x72}, {0x0070, 0x2A}, {0x0071, 0x46},
{0x0072, 0x55}, {0x0080, 0xC2}, {0x0082, 0xA2}, {0x0083, 0xF0},
{0x0085, 0x10}, {0x0086, 0x22}, {0x0087, 0x08}, {0x0088, 0x6D},
{0x0089, 0x2A}, {0x008A, 0x2F}, {0x008D, 0x20}, {0x0090, 0x01},
{0x0091, 0x02}, {0x0092, 0x03}, {0x0093, 0x04}, {0x0094, 0x14},
{0x0095, 0x09}, {0x0096, 0x0A}, {0x0097, 0x0B}, {0x0098, 0x0C},
{0x0099, 0x04}, {0x009A, 0x14}, {0x009B, 0x34}, {0x00A0, 0x00},
{0x00A1, 0x00}, {0x0B3B, 0x0B}, {0x0040, 0x0A}, {0x0053, 0x0A},
{0x0120, 0x37}, {0x0121, 0x80}, {0x0122, 0xAB}, //0xEB
{0x0123, 0xCC}, {0x0124, 0xDE}, {0x0125, 0xDF}, {0x0126, 0x70},
{0x0128, 0x1F}, {0x0132, 0xF8}, {0x011F, 0x08}, {0x0144, 0x04},
{0x0145, 0x00}, {0x0146, 0x20}, {0x0147, 0x20}, {0x0148, 0x14},
{0x0149, 0x14}, {0x0156, 0x0C}, {0x0157, 0x0C}, {0x0158, 0x0A},
{0x0159, 0x0A}, {0x015A, 0x03}, {0x015B, 0x40}, {0x015C, 0x21},
{0x015E, 0x0F}, {0x0168, 0xC8}, {0x0169, 0xC8}, {0x016A, 0x96},
{0x016B, 0x96}, {0x016C, 0x64}, {0x016D, 0x64}, {0x016E, 0x32},
{0x016F, 0x32}, {0x01EF, 0xF1}, {0x0131, 0x44}, {0x014C, 0x60},
{0x014D, 0x24}, {0x015D, 0x90}, {0x01D8, 0x40}, {0x01D9, 0x20},
{0x01DA, 0x23}, {0x0150, 0x05}, {0x0155, 0x07}, {0x0178, 0x10},
{0x017A, 0x10}, {0x01BA, 0x10}, {0x0176, 0x00}, {0x0179, 0x10},
{0x017B, 0x10}, {0x01BB, 0x10}, {0x0177, 0x00}, {0x01E7, 0x20},
{0x01E8, 0x30}, {0x01E9, 0x50}, {0x01E4, 0x18}, {0x01E5, 0x20},
{0x01E6, 0x04}, {0x0210, 0x21}, {0x0211, 0x0A}, {0x0212, 0x21},
{0x01DB, 0x04}, {0x01DC, 0x14}, {0x0151, 0x08}, {0x01F2, 0x18},
{0x01F8, 0x3C}, {0x01FE, 0x24}, {0x0213, 0x03}, {0x0214, 0x03},
{0x0215, 0x10}, {0x0216, 0x08}, {0x0217, 0x05}, {0x0218, 0xB8},
{0x0219, 0x01}, {0x021A, 0xB8}, {0x021B, 0x01}, {0x021C, 0xB8},
{0x021D, 0x01}, {0x021E, 0xB8}, {0x021F, 0x01}, {0x0220, 0xF1},
{0x0221, 0x5D}, {0x0222, 0x0A}, {0x0223, 0x80}, {0x0224, 0x50},
{0x0225, 0x09}, {0x0226, 0x80}, {0x022A, 0x56}, {0x022B, 0x13},
{0x022C, 0x80}, {0x022D, 0x11}, {0x022E, 0x08}, {0x022F, 0x11},
{0x0230, 0x08}, {0x0233, 0x11}, {0x0234, 0x08}, {0x0235, 0x88},
{0x0236, 0x02}, {0x0237, 0x88}, {0x0238, 0x02}, {0x023B, 0x88},
{0x023C, 0x02}, {0x023D, 0x68}, {0x023E, 0x01}, {0x023F, 0x68},
{0x0240, 0x01}, {0x0243, 0x68}, {0x0244, 0x01}, {0x0251, 0x0F},
{0x0252, 0x00}, {0x0260, 0x00}, {0x0261, 0x4A}, {0x0262, 0x2C},
{0x0263, 0x68}, {0x0264, 0x40}, {0x0265, 0x2C}, {0x0266, 0x6A},
{0x026A, 0x40}, {0x026B, 0x30}, {0x026C, 0x66}, {0x0278, 0x98},
{0x0279, 0x20}, {0x027A, 0x80}, {0x027B, 0x73}, {0x027C, 0x08},
{0x027D, 0x80}, {0x0280, 0x0D}, {0x0282, 0x1A}, {0x0284, 0x30},
{0x0286, 0x53}, {0x0288, 0x62}, {0x028a, 0x6E}, {0x028c, 0x7A},
{0x028e, 0x83}, {0x0290, 0x8B}, {0x0292, 0x92}, {0x0294, 0x9D},
{0x0296, 0xA8}, {0x0298, 0xBC}, {0x029a, 0xCF}, {0x029c, 0xE2},
{0x029e, 0x2A}, {0x02A0, 0x02}, {0x02C0, 0x7D}, {0x02C1, 0x01},
{0x02C2, 0x7C}, {0x02C3, 0x04}, {0x02C4, 0x01}, {0x02C5, 0x04},
{0x02C6, 0x3E}, {0x02C7, 0x04}, {0x02C8, 0x90}, {0x02C9, 0x01},
{0x02CA, 0x52}, {0x02CB, 0x04}, {0x02CC, 0x04}, {0x02CD, 0x04},
{0x02CE, 0xA9}, {0x02CF, 0x04}, {0x02D0, 0xAD}, {0x02D1, 0x01},
{0x0302, 0x00}, {0x0303, 0x00}, {0x0304, 0x00}, {0x02e0, 0x04},
{0x02F0, 0x4E}, {0x02F1, 0x04}, {0x02F2, 0xB1}, {0x02F3, 0x00},
{0x02F4, 0x63}, {0x02F5, 0x04}, {0x02F6, 0x28}, {0x02F7, 0x04},
{0x02F8, 0x29}, {0x02F9, 0x04}, {0x02FA, 0x51}, {0x02FB, 0x00},
{0x02FC, 0x64}, {0x02FD, 0x04}, {0x02FE, 0x6B}, {0x02FF, 0x04},
{0x0300, 0xCF}, {0x0301, 0x00}, {0x0305, 0x08}, {0x0306, 0x40},
{0x0307, 0x00}, {0x032D, 0x70}, {0x032E, 0x01}, {0x032F, 0x00},
{0x0330, 0x01}, {0x0331, 0x70}, {0x0332, 0x01}, {0x0333, 0x82},
{0x0334, 0x82}, {0x0335, 0x86}, {0x0340, 0x30}, {0x0341, 0x44},
{0x0342, 0x4A}, {0x0343, 0x3C}, {0x0344, 0x83}, {0x0345, 0x4D},
{0x0346, 0x75}, {0x0347, 0x56}, {0x0348, 0x68}, {0x0349, 0x5E},
{0x034A, 0x5C}, {0x034B, 0x65}, {0x034C, 0x52}, {0x0350, 0x88},
{0x0352, 0x18}, {0x0354, 0x80}, {0x0355, 0x50}, {0x0356, 0x88},
{0x0357, 0xE0}, {0x0358, 0x00}, {0x035A, 0x00}, {0x035B, 0xAC},
{0x0360, 0x02}, {0x0361, 0x18}, {0x0362, 0x50}, {0x0363, 0x6C},
{0x0364, 0x00}, {0x0365, 0xF0}, {0x0366, 0x08}, {0x036A, 0x10},
{0x036B, 0x18}, {0x036E, 0x10}, {0x0370, 0x10}, {0x0371, 0x18},
{0x0372, 0x0C}, {0x0373, 0x38}, {0x0374, 0x3A}, {0x0375, 0x12},
{0x0376, 0x20}, {0x0380, 0xFF}, {0x0381, 0x44}, {0x0382, 0x34},
{0x038A, 0x80}, {0x038B, 0x0A}, {0x038C, 0xC1}, {0x038E, 0x3C},
{0x038F, 0x09}, {0x0390, 0xE0}, {0x0391, 0x01}, {0x0392, 0x03},
{0x0393, 0x80}, {0x0395, 0x22}, {0x0398, 0x02}, {0x0399, 0xF0},
{0x039A, 0x03}, {0x039B, 0xAC}, {0x039C, 0x04}, {0x039D, 0x68},
{0x039E, 0x05}, {0x039F, 0xE0}, {0x03A0, 0x07}, {0x03A1, 0x58},
{0x03A2, 0x08}, {0x03A3, 0xD0}, {0x03A4, 0x0B}, {0x03A5, 0xC0},
{0x03A6, 0x18}, {0x03A7, 0x1C}, {0x03A8, 0x20}, {0x03A9, 0x24},
{0x03AA, 0x28}, {0x03AB, 0x30}, {0x03AC, 0x24}, {0x03AD, 0x21},
{0x03AE, 0x1C}, {0x03AF, 0x18}, {0x03B0, 0x17}, {0x03B1, 0x13},
{0x03B7, 0x64}, {0x03B8, 0x00}, {0x03B9, 0xB4}, {0x03BA, 0x00},
{0x03bb, 0xff}, {0x03bc, 0xff}, {0x03bd, 0xff}, {0x03be, 0xff},
{0x03bf, 0xff}, {0x03c0, 0xff}, {0x03c1, 0x01}, {0x03e0, 0x04},
{0x03e1, 0x11}, {0x03e2, 0x01}, {0x03e3, 0x04}, {0x03e4, 0x10},
{0x03e5, 0x21}, {0x03e6, 0x11}, {0x03e7, 0x00}, {0x03e8, 0x11},
{0x03e9, 0x32}, {0x03ea, 0x12}, {0x03eb, 0x01}, {0x03ec, 0x21},
{0x03ed, 0x33}, {0x03ee, 0x23}, {0x03ef, 0x01}, {0x03f0, 0x11},
{0x03f1, 0x32}, {0x03f2, 0x12}, {0x03f3, 0x01}, {0x03f4, 0x10},
{0x03f5, 0x21}, {0x03f6, 0x11}, {0x03f7, 0x00}, {0x03f8, 0x04},
{0x03f9, 0x11}, {0x03fa, 0x01}, {0x03fb, 0x04}, {0x03DC, 0x47},
{0x03DD, 0x5A}, {0x03DE, 0x41}, {0x03DF, 0x53}, {0x0420, 0x82},
{0x0421, 0x00}, {0x0422, 0x00}, {0x0423, 0x88}, {0x0430, 0x08},
{0x0431, 0x30}, {0x0432, 0x0c}, {0x0433, 0x04}, {0x0435, 0x08},
{0x0450, 0xFF}, {0x0451, 0xD0}, {0x0452, 0xB8}, {0x0453, 0x88},
{0x0454, 0x00}, {0x0458, 0x80}, {0x0459, 0x03}, {0x045A, 0x00},
{0x045B, 0x50}, {0x045C, 0x00}, {0x045D, 0x90}, {0x0465, 0x02},
{0x0466, 0x14}, {0x047A, 0x00}, {0x047B, 0x00}, {0x047C, 0x04},
{0x047D, 0x50}, {0x047E, 0x04}, {0x047F, 0x90}, {0x0480, 0x58},
{0x0481, 0x06}, {0x0482, 0x08}, {0x04B0, 0x50}, {0x04B6, 0x30},
{0x04B9, 0x10}, {0x04B3, 0x00}, {0x04B1, 0x85}, {0x04B4, 0x00},
{0x0540, 0x00}, {0x0541, 0xBC}, {0x0542, 0x00}, {0x0543, 0xE1},
{0x0580, 0x04}, {0x0581, 0x0F}, {0x0582, 0x04}, {0x05A1, 0x0A},
{0x05A2, 0x21}, {0x05A3, 0x84}, {0x05A4, 0x24}, {0x05A5, 0xFF},
{0x05A6, 0x00}, {0x05A7, 0x24}, {0x05A8, 0x24}, {0x05A9, 0x02},
{0x05B1, 0x24}, {0x05B2, 0x0C}, {0x05B4, 0x1F}, {0x05AE, 0x75},
{0x05AF, 0x78}, {0x05B6, 0x00}, {0x05B7, 0x10}, {0x05BF, 0x20},
{0x05C1, 0x06}, {0x05C2, 0x18}, {0x05C7, 0x00}, {0x05CC, 0x04},
{0x05CD, 0x00}, {0x05CE, 0x03}, {0x05E4, 0x08}, {0x05E5, 0x00},
{0x05E6, 0x07}, {0x05E7, 0x05}, {0x05E8, 0x06}, {0x05E9, 0x00},
{0x05EA, 0x25}, {0x05EB, 0x03}, {0x0660, 0x00}, {0x0661, 0x16},
{0x0662, 0x07}, {0x0663, 0xf1}, {0x0664, 0x07}, {0x0665, 0xde},
{0x0666, 0x07}, {0x0667, 0xe7}, {0x0668, 0x00}, {0x0669, 0x35},
{0x066a, 0x07}, {0x066b, 0xf9}, {0x066c, 0x07}, {0x066d, 0xb7},
{0x066e, 0x00}, {0x066f, 0x27}, {0x0670, 0x07}, {0x0671, 0xf3},
{0x0672, 0x07}, {0x0673, 0xc5}, {0x0674, 0x07}, {0x0675, 0xee},
{0x0676, 0x00}, {0x0677, 0x16}, {0x0678, 0x01}, {0x0679, 0x80},
{0x067a, 0x00}, {0x067b, 0x85}, {0x067c, 0x07}, {0x067d, 0xe1},
{0x067e, 0x07}, {0x067f, 0xf5}, {0x0680, 0x07}, {0x0681, 0xb9},
{0x0682, 0x00}, {0x0683, 0x31}, {0x0684, 0x07}, {0x0685, 0xe6},
{0x0686, 0x07}, {0x0687, 0xd3}, {0x0688, 0x00}, {0x0689, 0x18},
{0x068a, 0x07}, {0x068b, 0xfa}, {0x068c, 0x07}, {0x068d, 0xd2},
{0x068e, 0x00}, {0x068f, 0x08}, {0x0690, 0x00}, {0x0691, 0x02},
{0xAFD0, 0x03}, {0xAFD3, 0x18}, {0xAFD4, 0x04}, {0xAFD5, 0xB8},
{0xAFD6, 0x02}, {0xAFD7, 0x44}, {0xAFD8, 0x02},
{0x0000, 0x01}, //
{0x0100, 0x01}, //
{0x0101, 0x01}, //
{0x0005, 0x01}, // Turn on rolling shutter

#ifdef CONFIG_FLICKER_50HZ_DEV1
	{0x0542, 0x00},
	{0x0543, 0xE1},
#endif
#ifdef CONFIG_FLICKER_60HZ_DEV1
	{0x0540, 0x00},
	{0x0541, 0xBC},
#endif
};

#if 0
static struct NT_RegValue hm1055_setting_YUV_VGA[] = {
{0x0022, 0x00}, {0x0023, 0xCF}, {0x0020, 0x08}, {0x0027, 0x30},
{0x0004, 0x10}, {0x0006, 0x03}, {0x0012, 0x0F},
/* {0x0026, 0x77}, */ /*48Mhz */
{0x0026, 0x37}, /*68Mhz */
{0x002A, 0x44}, {0x002B, 0x01}, {0x002C, 0x00}, /* {0x0025, 0x00}, */
{0x004A, 0x0A}, {0x004B, 0x72}, {0x0070, 0x2A}, {0x0071, 0x46},
{0x0072, 0x55}, {0x0080, 0xC2}, {0x0082, 0xA2}, {0x0083, 0xF0},
{0x0085, 0x10}, {0x0086, 0x22}, {0x0087, 0x08}, {0x0088, 0x6D},
{0x0089, 0x2A}, {0x008A, 0x2F}, {0x008D, 0x20}, {0x0090, 0x01},
{0x0091, 0x02}, {0x0092, 0x03}, {0x0093, 0x04}, {0x0094, 0x14},
{0x0095, 0x09}, {0x0096, 0x0A}, {0x0097, 0x0B}, {0x0098, 0x0C},
{0x0099, 0x04}, {0x009A, 0x14}, {0x009B, 0x34}, {0x00A0, 0x00},
{0x00A1, 0x00}, {0x0B3B, 0x0B}, {0x0040, 0x0A}, {0x0053, 0x0A},
{0x0120, 0x37}, {0x0121, 0x80}, {0x0122, 0xAB}, //0xEB
{0x0123, 0xCC}, {0x0124, 0xDE}, {0x0125, 0xDF}, {0x0126, 0x70},
{0x0128, 0x1F}, {0x0132, 0xF8}, {0x011F, 0x08}, {0x0144, 0x04},
{0x0145, 0x00}, {0x0146, 0x20}, {0x0147, 0x20}, {0x0148, 0x14},
{0x0149, 0x14}, {0x0156, 0x0C}, {0x0157, 0x0C}, {0x0158, 0x0A},
{0x0159, 0x0A}, {0x015A, 0x03}, {0x015B, 0x40}, {0x015C, 0x21},
{0x015E, 0x0F}, {0x0168, 0xC8}, {0x0169, 0xC8}, {0x016A, 0x96},
{0x016B, 0x96}, {0x016C, 0x64}, {0x016D, 0x64}, {0x016E, 0x32},
{0x016F, 0x32}, {0x01EF, 0xF1}, {0x0131, 0x44}, {0x014C, 0x60},
{0x014D, 0x24}, {0x015D, 0x90}, {0x01D8, 0x40}, {0x01D9, 0x20},
{0x01DA, 0x23}, {0x0150, 0x05}, {0x0155, 0x07}, {0x0178, 0x10},
{0x017A, 0x10}, {0x01BA, 0x10}, {0x0176, 0x00}, {0x0179, 0x10},
{0x017B, 0x10}, {0x01BB, 0x10}, {0x0177, 0x00}, {0x01E7, 0x20},
{0x01E8, 0x30}, {0x01E9, 0x50}, {0x01E4, 0x18}, {0x01E5, 0x20},
{0x01E6, 0x04}, {0x0210, 0x21}, {0x0211, 0x0A}, {0x0212, 0x21},
{0x01DB, 0x04}, {0x01DC, 0x14}, {0x0151, 0x08}, {0x01F2, 0x18},
{0x01F8, 0x3C}, {0x01FE, 0x24}, {0x0213, 0x03}, {0x0214, 0x03},
{0x0215, 0x10}, {0x0216, 0x08}, {0x0217, 0x05}, {0x0218, 0xB8},
{0x0219, 0x01}, {0x021A, 0xB8}, {0x021B, 0x01}, {0x021C, 0xB8},
{0x021D, 0x01}, {0x021E, 0xB8}, {0x021F, 0x01}, {0x0220, 0xF1},
{0x0221, 0x5D}, {0x0222, 0x0A}, {0x0223, 0x80}, {0x0224, 0x50},
{0x0225, 0x09}, {0x0226, 0x80}, {0x022A, 0x56}, {0x022B, 0x13},
{0x022C, 0x80}, {0x022D, 0x11}, {0x022E, 0x08}, {0x022F, 0x11},
{0x0230, 0x08}, {0x0233, 0x11}, {0x0234, 0x08}, {0x0235, 0x88},
{0x0236, 0x02}, {0x0237, 0x88}, {0x0238, 0x02}, {0x023B, 0x88},
{0x023C, 0x02}, {0x023D, 0x68}, {0x023E, 0x01}, {0x023F, 0x68},
{0x0240, 0x01}, {0x0243, 0x68}, {0x0244, 0x01}, {0x0251, 0x0F},
{0x0252, 0x00}, {0x0260, 0x00}, {0x0261, 0x4A}, {0x0262, 0x2C},
{0x0263, 0x68}, {0x0264, 0x40}, {0x0265, 0x2C}, {0x0266, 0x6A},
{0x026A, 0x40}, {0x026B, 0x30}, {0x026C, 0x66}, {0x0278, 0x98},
{0x0279, 0x20}, {0x027A, 0x80}, {0x027B, 0x73}, {0x027C, 0x08},
{0x027D, 0x80}, {0x0280, 0x0D}, {0x0282, 0x1A}, {0x0284, 0x30},
{0x0286, 0x53}, {0x0288, 0x62}, {0x028a, 0x6E}, {0x028c, 0x7A},
{0x028e, 0x83}, {0x0290, 0x8B}, {0x0292, 0x92}, {0x0294, 0x9D},
{0x0296, 0xA8}, {0x0298, 0xBC}, {0x029a, 0xCF}, {0x029c, 0xE2},
{0x029e, 0x2A}, {0x02A0, 0x02}, {0x02C0, 0x7D}, {0x02C1, 0x01},
{0x02C2, 0x7C}, {0x02C3, 0x04}, {0x02C4, 0x01}, {0x02C5, 0x04},
{0x02C6, 0x3E}, {0x02C7, 0x04}, {0x02C8, 0x90}, {0x02C9, 0x01},
{0x02CA, 0x52}, {0x02CB, 0x04}, {0x02CC, 0x04}, {0x02CD, 0x04},
{0x02CE, 0xA9}, {0x02CF, 0x04}, {0x02D0, 0xAD}, {0x02D1, 0x01},
{0x0302, 0x00}, {0x0303, 0x00}, {0x0304, 0x00}, {0x02e0, 0x04},
{0x02F0, 0x4E}, {0x02F1, 0x04}, {0x02F2, 0xB1}, {0x02F3, 0x00},
{0x02F4, 0x63}, {0x02F5, 0x04}, {0x02F6, 0x28}, {0x02F7, 0x04},
{0x02F8, 0x29}, {0x02F9, 0x04}, {0x02FA, 0x51}, {0x02FB, 0x00},
{0x02FC, 0x64}, {0x02FD, 0x04}, {0x02FE, 0x6B}, {0x02FF, 0x04},
{0x0300, 0xCF}, {0x0301, 0x00}, {0x0305, 0x08}, {0x0306, 0x40},
{0x0307, 0x00}, {0x032D, 0x70}, {0x032E, 0x01}, {0x032F, 0x00},
{0x0330, 0x01}, {0x0331, 0x70}, {0x0332, 0x01}, {0x0333, 0x82},
{0x0334, 0x82}, {0x0335, 0x86}, {0x0340, 0x30}, {0x0341, 0x44},
{0x0342, 0x4A}, {0x0343, 0x3C}, {0x0344, 0x83}, {0x0345, 0x4D},
{0x0346, 0x75}, {0x0347, 0x56}, {0x0348, 0x68}, {0x0349, 0x5E},
{0x034A, 0x5C}, {0x034B, 0x65}, {0x034C, 0x52}, {0x0350, 0x88},
{0x0352, 0x18}, {0x0354, 0x80}, {0x0355, 0x50}, {0x0356, 0x88},
{0x0357, 0xE0}, {0x0358, 0x00}, {0x035A, 0x00}, {0x035B, 0xAC},
{0x0360, 0x02}, {0x0361, 0x18}, {0x0362, 0x50}, {0x0363, 0x6C},
{0x0364, 0x00}, {0x0365, 0xF0}, {0x0366, 0x08}, {0x036A, 0x10},
{0x036B, 0x18}, {0x036E, 0x10}, {0x0370, 0x10}, {0x0371, 0x18},
{0x0372, 0x0C}, {0x0373, 0x38}, {0x0374, 0x3A}, {0x0375, 0x12},
{0x0376, 0x20}, {0x0380, 0xFF}, {0x0381, 0x44}, {0x0382, 0x34},
{0x038A, 0x80}, {0x038B, 0x0A}, {0x038C, 0xC1}, {0x038E, 0x3C},
{0x038F, 0x09}, {0x0390, 0xE0}, {0x0391, 0x01}, {0x0392, 0x03},
{0x0393, 0x80}, {0x0395, 0x22}, {0x0398, 0x02}, {0x0399, 0xF0},
{0x039A, 0x03}, {0x039B, 0xAC}, {0x039C, 0x04}, {0x039D, 0x68},
{0x039E, 0x05}, {0x039F, 0xE0}, {0x03A0, 0x07}, {0x03A1, 0x58},
{0x03A2, 0x08}, {0x03A3, 0xD0}, {0x03A4, 0x0B}, {0x03A5, 0xC0},
{0x03A6, 0x18}, {0x03A7, 0x1C}, {0x03A8, 0x20}, {0x03A9, 0x24},
{0x03AA, 0x28}, {0x03AB, 0x30}, {0x03AC, 0x24}, {0x03AD, 0x21},
{0x03AE, 0x1C}, {0x03AF, 0x18}, {0x03B0, 0x17}, {0x03B1, 0x13},
{0x03B7, 0x64}, {0x03B8, 0x00}, {0x03B9, 0xB4}, {0x03BA, 0x00},
{0x03bb, 0xff}, {0x03bc, 0xff}, {0x03bd, 0xff}, {0x03be, 0xff},
{0x03bf, 0xff}, {0x03c0, 0xff}, {0x03c1, 0x01}, {0x03e0, 0x04},
{0x03e1, 0x11}, {0x03e2, 0x01}, {0x03e3, 0x04}, {0x03e4, 0x10},
{0x03e5, 0x21}, {0x03e6, 0x11}, {0x03e7, 0x00}, {0x03e8, 0x11},
{0x03e9, 0x32}, {0x03ea, 0x12}, {0x03eb, 0x01}, {0x03ec, 0x21},
{0x03ed, 0x33}, {0x03ee, 0x23}, {0x03ef, 0x01}, {0x03f0, 0x11},
{0x03f1, 0x32}, {0x03f2, 0x12}, {0x03f3, 0x01}, {0x03f4, 0x10},
{0x03f5, 0x21}, {0x03f6, 0x11}, {0x03f7, 0x00}, {0x03f8, 0x04},
{0x03f9, 0x11}, {0x03fa, 0x01}, {0x03fb, 0x04}, {0x03DC, 0x47},
{0x03DD, 0x5A}, {0x03DE, 0x41}, {0x03DF, 0x53}, {0x0420, 0x82},
{0x0421, 0x00}, {0x0422, 0x00}, {0x0423, 0x88}, {0x0430, 0x08},
{0x0431, 0x30}, {0x0432, 0x0c}, {0x0433, 0x04}, {0x0435, 0x08},
{0x0450, 0xFF}, {0x0451, 0xD0}, {0x0452, 0xB8}, {0x0453, 0x88},
{0x0454, 0x00}, {0x0458, 0x80}, {0x0459, 0x03}, {0x045A, 0x00},
{0x045B, 0x50}, {0x045C, 0x00}, {0x045D, 0x90}, {0x0465, 0x02},
{0x0466, 0x14}, {0x047A, 0x00}, {0x047B, 0x00}, {0x047C, 0x04},
{0x047D, 0x50}, {0x047E, 0x04}, {0x047F, 0x90}, {0x0480, 0x58},
{0x0481, 0x06}, {0x0482, 0x08}, {0x04B0, 0x50}, {0x04B6, 0x30},
{0x04B9, 0x10}, {0x04B3, 0x00}, {0x04B1, 0x85}, {0x04B4, 0x00},
{0x0540, 0x00}, {0x0541, 0xBC}, {0x0542, 0x00}, {0x0543, 0xE1},
{0x0580, 0x04}, {0x0581, 0x0F}, {0x0582, 0x04}, {0x05A1, 0x0A},
{0x05A2, 0x21}, {0x05A3, 0x84}, {0x05A4, 0x24}, {0x05A5, 0xFF},
{0x05A6, 0x00}, {0x05A7, 0x24}, {0x05A8, 0x24}, {0x05A9, 0x02},
{0x05B1, 0x24}, {0x05B2, 0x0C}, {0x05B4, 0x1F}, {0x05AE, 0x75},
{0x05AF, 0x78}, {0x05B6, 0x00}, {0x05B7, 0x10}, {0x05BF, 0x20},
{0x05C1, 0x06}, {0x05C2, 0x18}, {0x05C7, 0x00}, {0x05CC, 0x04},
{0x05CD, 0x00}, {0x05CE, 0x03}, {0x05E4, 0x08}, {0x05E5, 0x00},
{0x05E6, 0x07}, {0x05E7, 0x05}, {0x05E8, 0x06}, {0x05E9, 0x00},
{0x05EA, 0x25}, {0x05EB, 0x03}, {0x0660, 0x00}, {0x0661, 0x16},
{0x0662, 0x07}, {0x0663, 0xf1}, {0x0664, 0x07}, {0x0665, 0xde},
{0x0666, 0x07}, {0x0667, 0xe7}, {0x0668, 0x00}, {0x0669, 0x35},
{0x066a, 0x07}, {0x066b, 0xf9}, {0x066c, 0x07}, {0x066d, 0xb7},
{0x066e, 0x00}, {0x066f, 0x27}, {0x0670, 0x07}, {0x0671, 0xf3},
{0x0672, 0x07}, {0x0673, 0xc5}, {0x0674, 0x07}, {0x0675, 0xee},
{0x0676, 0x00}, {0x0677, 0x16}, {0x0678, 0x01}, {0x0679, 0x80},
{0x067a, 0x00}, {0x067b, 0x85}, {0x067c, 0x07}, {0x067d, 0xe1},
{0x067e, 0x07}, {0x067f, 0xf5}, {0x0680, 0x07}, {0x0681, 0xb9},
{0x0682, 0x00}, {0x0683, 0x31}, {0x0684, 0x07}, {0x0685, 0xe6},
{0x0686, 0x07}, {0x0687, 0xd3}, {0x0688, 0x00}, {0x0689, 0x18},
{0x068a, 0x07}, {0x068b, 0xfa}, {0x068c, 0x07}, {0x068d, 0xd2},
{0x068e, 0x00}, {0x068f, 0x08}, {0x0690, 0x00}, {0x0691, 0x02},
{0xAFD0, 0x03}, {0xAFD3, 0x18}, {0xAFD4, 0x04}, {0xAFD5, 0xB8},
{0xAFD6, 0x02}, {0xAFD7, 0x44}, {0xAFD8, 0x02},
{0x0000, 0x01}, //
{0x0100, 0x01}, //
{0x0101, 0x01}, //
{0x0005, 0x01}, // Turn on rolling shutter

{0x002B, 0x01}, {0x0023, 0xCF}, {0x0027, 0x30}, {0x0005, 0x00},
{0x0006, 0x10}, {0x000D, 0x00}, {0x000E, 0x00}, {0x0122, 0x6B},
{0x0125, 0xFF}, {0x0126, 0x70}, {0x05E0, 0xC1}, {0x05E1, 0x00},
{0x05E2, 0xC1}, {0x05E3, 0x00}, {0x05E4, 0x03}, {0x05E5, 0x00},
{0x05E6, 0x82}, {0x05E7, 0x02}, {0x05E8, 0x04}, {0x05E9, 0x00},
{0x05EA, 0xE3}, {0x05EB, 0x01}, {0x0000, 0x01},	{0x0100, 0x01},
{0x0101, 0x01},	{0x0005, 0x01},
#ifdef CONFIG_FLICKER_50HZ_DEV1
	{0x0542, 0x00},
	{0x0543, 0xE1},
#endif
#ifdef CONFIG_FLICKER_60HZ_DEV1
	{0x0540, 0x00},
	{0x0541, 0xBC},
#endif
};
#endif

static void Delay(uint32_t nCount)
{
    volatile uint32_t i;
    for(; nCount!=0; nCount--)
        for(i=0; i<400; i++);
}

static void hm1055_SnrReset(CCAP_T* ccap)
{
	if(ccap == CCAP0) {
		/* PM1 reset:	H->L->H 	*/
		SYS->GPM_MFPL &= ~SYS_GPM_MFPL_PM1MFP_Msk;			/* PM1 for #RST */
		GPIO_SetMode(PM,1<<1,GPIO_MODE_OUTPUT);            /* Set #RST pin to high */
		PM1=1;
		Delay(100);
		PM1=0;
		Delay(100);
		PM1=1;
	} else {
		/* PN14 reset:	H->L->H 	*/
		SYS->GPN_MFPH &= ~SYS_GPN_MFPH_PN14MFP_Msk;			/* PN14 for #RST */
		GPIO_SetMode(PN,1<<14,GPIO_MODE_OUTPUT);            /* Set #RST pin to high */
		PN14=1;
		Delay(100);
		PN14=0;
		Delay(100);
		PN14=1;
	}
}

static void hm1055_SnrPowerDown(CCAP_T* ccap, int bIsEnable)
{
	if(ccap == CCAP0) {
		SYS->GPK_MFPH &= ~SYS_GPK_MFPH_PK8MFP_Msk;			/* PK8 for #PD */
		GPIO_SetMode(PK,1<<8,GPIO_MODE_OUTPUT);				/* Set #PD pin to low */
		PK8=0;
		Delay(100);
		if(bIsEnable)
			PK8=1;
		else
			PK8=0;
	} else {
		SYS->GPD_MFPH &= ~SYS_GPD_MFPH_PD15MFP_Msk;			/* PD15 for #PD */
		GPIO_SetMode(PD,1<<15,GPIO_MODE_OUTPUT);			/* Set #PD pin to low */
		PD15=0;
		Delay(100);
		if(bIsEnable)
			PD15=1;
		else
			PD15=0;
    }
}

int InitHM1055_VGA_YUV422(CCAP_T* ccap)
{
    uint32_t i;
    uint8_t u8DeviceID=0x48;
    uint8_t u8ID[2]= {0};

    if(ccap == CCAP0) {
    	/* CCAP0 in NUMAKER_MA35D1_BASE_V11 */
    	SYS->GPM_MFPH &= ~SYS_GPM_MFPH_PM15MFP_Msk;        /* PM15 for GPIO to act as SCL */
    	SYS->GPM_MFPH &= ~SYS_GPM_MFPH_PM14MFP_Msk;        /* PM14 for GPIO to act as SDA */

    	hm1055_SnrReset(ccap);
    	hm1055_SnrPowerDown(ccap, FALSE);

		/* switch I2C pin function, to do... */
		SWI2C_Open(eDRVGPIO_GPIOM,eDRVGPIO_PIN15,eDRVGPIO_GPIOM,eDRVGPIO_PIN14,Delay);
	} else {
		/* CCAP1 in NUMAKER_MA35D1_BASE_V11 */
		SYS->GPL_MFPL &= ~SYS_GPL_MFPL_PL5MFP_Msk;        /* PL5 for GPIO to act as SCL */
		SYS->GPL_MFPL &= ~SYS_GPL_MFPL_PL4MFP_Msk;        /* PL4 for GPIO to act as SDA */


    	hm1055_SnrReset(ccap);
    	hm1055_SnrPowerDown(ccap, FALSE);

		/* switch I2C pin function, to do... */
		SWI2C_Open(eDRVGPIO_GPIOL,eDRVGPIO_PIN5,eDRVGPIO_GPIOL,eDRVGPIO_PIN4,Delay);
	}
    sysprintf("NT_RegNum=%3d\n",sizeof(hm1055_setting_YUV_720P)/sizeof(struct NT_RegValue));
    for(i=0; i<sizeof(hm1055_setting_YUV_720P)/sizeof(struct NT_RegValue); i++)
    {
        SWI2C_Write_8bitSlaveAddr_16bitReg_8bitData(u8DeviceID,hm1055_setting_YUV_720P[i].u16RegAddr,hm1055_setting_YUV_720P[i].u8Value);
    }
    u8ID[0]=SWI2C_Read_8bitSlaveAddr_16bitReg_8bitData(u8DeviceID,0x0001);  /* Chip_Version_H 0x09 */
    u8ID[1]=SWI2C_Read_8bitSlaveAddr_16bitReg_8bitData(u8DeviceID,0x0002);  /* Chip_Version_L 0x55 */
    sysprintf("Sensor Chip_Version_H = 0x%02x(0x09) Chip_Version_L = 0x%02x(0x55)\n", u8ID[0],u8ID[1]);
    if(u8ID[0]!=0x09 || u8ID[1]!=0x55 )
    {
    	sysprintf("HM1055 init failed!!\n");
        return 0;
    }
    return 1;
}




